package com.iptv.grape.ui.dlg;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.core.content.ContextCompat;

import com.iptv.grape.R;

public class CstmDlg extends Dialog implements View.OnClickListener {
    //在构造方法里提前加载了样式
    private Context context;//上下文
    private int layout;
    private int width ;
    private int height ;
    private int gravity ;
    private boolean outside ;
    private CstmListener listener;
    private int dlgBg;

    public static class Builder{
        private Context ctx;
        private int style = R.style.MeDlg;
        private int layout = R.layout.dlg_cstm;
        private int width =  WindowManager.LayoutParams.WRAP_CONTENT;
        private int height =  WindowManager.LayoutParams.WRAP_CONTENT;
        private int gravity =   Gravity.CENTER;
        private boolean outside = false;
        private CstmListener listener;
        private int dlgBg = R.drawable.dlg_apktreat_bg;
        public Builder(Context ctx){
            this.ctx = ctx;
        }
        public Builder style(int style){
            this.style = style;
            return this;
        }
        public Builder layout(int layout){
            this.layout = layout;
            return this;
        }
        public Builder width(int width){
            this.width = width;
            return this;
        }
        public Builder height(int height){
            this.height = height;
            return this;
        }

        public Builder gravity(int gravity){
            this.gravity = gravity;
            return this;
        }
        public Builder outside(boolean outside){
            this.outside = outside;
            return this;
        }
        public Builder dlgBg(int dlgBg){
            this.dlgBg = dlgBg;
            return this;
        }
        public Builder listener(CstmListener listener){
            this.listener = listener;
            return this;
        }
        public CstmDlg build(){
               return new CstmDlg(this);
        }

    }

    public CstmDlg(Builder builder) {
        super(builder.ctx, builder.style);//加载dialog的样式
        this.context = builder.ctx;
        this.layout = builder.layout;
        this.width = builder.width;
        this.height = builder.height;
        this.gravity = builder.gravity;
        this.outside = builder.outside;
        this.listener = builder.listener;
        this.dlgBg = builder.dlgBg;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context,layout,null);
        setContentView(view);
        if(null!=listener) {
            listener.initView(context, view,this);
        }
        Window window = this.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = width;
        layoutParams.height =height;
        layoutParams.gravity =gravity;
        window.setAttributes(layoutParams);
        window.setLayout(width,height);
        window.setBackgroundDrawable(ContextCompat.getDrawable(context, dlgBg));//指定背景，下图中的2
        setCanceledOnTouchOutside(outside);//点击外部Dialog消失
    }
    @Override
    public void onClick(View v) {
        listener.click(v);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if(null!=listener)
            listener.dismiss();

    }


}